package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.Product;
import com.wanlian.entity.User;
import com.wanlian.entity.UserScore;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.ProductServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductServices productServices;

    /**
     * 添加商品
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data, HttpSession session) {
        Product product = JsonStringUtil.readValue(data, Product.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int uid = user.getLoginUser().getId();
        product.setUserId(uid);
        ResultModel resultModel = productServices.doAdd(product);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 修改商品
     *
     * @param data
     * @returnzx
     */
    @RequestMapping(value = "/modify", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String modify(@RequestBody String data) {
        Product product = JsonStringUtil.readValue(data, Product.class);
        ResultModel resultModel = productServices.doModify(product);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 启用禁用商品
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/enable", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String enableOrDelete(@RequestBody String data) {
        Product product = JsonStringUtil.readValue(data, Product.class);
        // if(product.isEnable()) {
        ResultModel resultModel = productServices.doEnable(product);
        return new JsonResult().Serailze(resultModel);
        /*}
        else{
            ResultModel resultModel = productServices.doDelete(product);
            return new JsonResult().Serailze(resultModel);
        }*/
    }

    /**
     * 模糊查询商品
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/name", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByName(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        String name = (String) pageCondition.getByKey("name");
        ResultModel resultModel = productServices.queryByName(name, pageCondition.getPage());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询商品
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/condition", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = productServices.queryByPageCondition(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 按id查询商品
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/info", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String productInfo(@RequestBody String data) {
        Product product = JsonStringUtil.readValue(data, Product.class);
        int id = product.getId();
        ResultModel resultModel = new ResultModel();
        resultModel.setObject(productServices.queryById(id));
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台
     * 兑换商品
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/exchange", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String exchangeProductApp(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        int uid = 0;
        ResultModel resultModel = productServices.addProduct(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * app平台
     * 兑换商品
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/exchange", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String exchangeProductApp(@RequestBody String data, HttpSession session) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if (user == null) {
            return new JsonResult().Serailze(false, ErrorCode.ERROR_SESSION_TAG, ErrorCode.USER_SESSION_NOT_TIME);
        }
        int uid = user.getLoginUser().getId();
        ResultModel resultModel = productServices.addProduct(pageCondition, uid);
        return new JsonResult().Serailze(resultModel);
    }
}
